<script setup lang="ts">
import { ref } from "vue";
import { useRole } from "./utils/hook";
import { PureTableBar } from "@/components/RePureTableBar";
import { useRenderIcon } from "@/components/ReIcon/src/hooks";
import { ArrowDown, Checked } from '@element-plus/icons-vue'

import Refresh from "~icons/ep/refresh";

defineOptions({
  name: "CustomerLabel"
});

const formRef = ref();
const {
  permissions,

  statusList,
  form,
  loading,
  columns,
  dataList,
  pagination,
  onSearch,
  resetForm,
  handleSizeChange,
  handleCurrentChange,
  handleCommand,
  openDialog,
} = useRole();
</script>

<template>
  <div class="main">
    <el-form
      ref="formRef"
      :inline="true"
      :model="form"
      class="search-form bg-bg_color w-full pl-8 pt-[12px] overflow-auto"
    >
      <el-form-item prop="key">
        <el-input
          v-model="form.key"
          placeholder="标签名称"
          clearable
          class="w-[180px]!"
        />
      </el-form-item>
      <el-form-item prop="status">
        <el-select
          v-model="form.status"
          placeholder="启用状态"
          clearable
          class="w-[120px]!"
        >
          <el-option v-for="(item, index) in statusList" :key="index" :label="item.label" :value="item.value" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          :icon="useRenderIcon('ri/search-line')"
          :loading="loading"
          @click="onSearch"
        >
          查询
        </el-button>
        <el-button :icon="useRenderIcon(Refresh)" @click="resetForm(formRef)">
          重置
        </el-button>
      </el-form-item>
      
      <el-form-item v-if="permissions.is_tag_add">
        <el-button
          type="primary"
          :icon="useRenderIcon('ri:add-large-line')"
          @click="openDialog()"
        >
          新增
        </el-button>
      </el-form-item>
    </el-form>

    <PureTableBar
      title=""
      :columns="columns"
      @refresh="onSearch"
      :key="columns.length"
    >
      <template v-slot="{ size, dynamicColumns }">
        <pure-table
          align-whole="center"
          showOverflowTooltip
          table-layout="auto"
          :loading="loading"
          :size="size"
          adaptive
          :adaptiveConfig="{ offsetBottom: 108 }"
          :data="dataList"
          :columns="dynamicColumns"
          :pagination="{ ...pagination, size }"
          :header-cell-style="{
            background: 'var(--el-fill-color-light)',
            color: 'var(--el-text-color-primary)'
          }"
          @page-size-change="handleSizeChange"
          @page-current-change="handleCurrentChange"
        >
          <template #operation="{ row }">
            <el-dropdown @command="handleCommand(row, $event)">
              <el-button type="primary" size="small">
                操作<el-icon class="el-icon--right"><ArrowDown /></el-icon>
              </el-button>
              <template #dropdown>
                <el-dropdown-menu>
                  <el-dropdown-item command="edit" v-if="permissions.is_tag_edit">编辑</el-dropdown-item>
                  <el-dropdown-item command="rise" v-if="permissions.is_tag_up">升序</el-dropdown-item>
                  <el-dropdown-item command="drop" v-if="permissions.is_tag_down">降序</el-dropdown-item>
                  <el-dropdown-item divided command="delete" v-if="permissions.is_tag_del">删除</el-dropdown-item>
                </el-dropdown-menu>
              </template>
            </el-dropdown>
          </template>
        </pure-table>
      </template>
    </PureTableBar>
  </div>
</template>

<style lang="scss" scoped>
:deep(.el-dropdown-menu__item i) {
  margin: 0;
}

:deep(.el-form--inline .el-form-item) {
  margin-right: 12px;
}

.multipleInner {
  background-color: var(--pure-theme-menu-active-before);
  height: 34px;
  display: flex;
  align-items: center;
  padding: 0 10px;
  font-size: 14px;
  color: #ffffff;

  b, p {
    margin-left: 3px;
  }

  .delete {
    margin-left: 20px;
    display: flex;
    align-items: center;
    cursor: pointer;
  }
}

.main {
  margin: 12px 12px 0 !important;
}

.search-form {
  :deep(.el-form-item) {
    margin-bottom: 12px;
  }
}
</style>
